package com.zzptc.MZBapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
     EditText et_mzb_name , et_mzb_pwd;
     Switch aSwitch_mzb;
     Button btn_mzb_longin;
     boolean flag=false;//标志//全局变量
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();//实例化控件
        eventListener();
    }
    private void eventListener() {
        //自动登录是否改变
        aSwitch_mzb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            //匿名类不类
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    flag=true;
                }else {
                    flag=false;
                }
                Toast.makeText(MainActivity.this,""+b,Toast.LENGTH_SHORT).show();
            }
        });
        btn_mzb_longin.setOnClickListener(new View.OnClickListener() {
            //获取数值
            String name=et_mzb_name.getText().toString().trim();
            String pwd=et_mzb_pwd.getText().toString().trim();

            //如果为真，就记住在data//记住(用户名，密码，是否自动登录)
            SharedPreferences sh= getSharedPreferences("MZB",MODE_PRIVATE);
            SharedPreferences .Editor edit =sh.edit();
            @Override
            public void onClick(View view) {
                if(flag){
                   edit.putString("uname",name);
                   edit.putString("password",pwd);
                   edit.putBoolean("autologin",flag);
                   edit.commit();
                }else {
                    edit.putString("uname","");
                    edit.putString("password","");
                    edit.putBoolean("autologin",flag);
                    edit.commit();
                }
                //登陆
                login(name,pwd);
            }
        });
    }
    //形参
    public void login(String name,String pwd){


    }
    private void init() {
        et_mzb_name=findViewById(R.id.et_mzb_name);
        et_mzb_pwd=findViewById(R.id.et_mzb_pwd);
        aSwitch_mzb=findViewById(R.id. aSwitch_mzb);
        btn_mzb_longin=findViewById(R.id. btn_mzb_longin);
    }
}
